﻿Public Class FlowList_ToolWindow

    Private _ParentForm As MainForm
    Private WithEvents _MetisCore As Core.MetisCore
    Private WithEvents _FuncList As Core.Functions.MetisFunctionList
    Private WithEvents _FlowList As Core.Flows.MetisFlowList

    Public Event ErrorOccurred(ByVal Message As String)

    Sub Init(ByVal MetisCore As Core.MetisCore, ByVal ParentForm As MainForm)
        _MetisCore = MetisCore
        _ParentForm = ParentForm
        If _MetisCore.Connection.State = Data.ConnectionState.Open Then Reload()
    End Sub

    Private Sub HandleFlowChildListChange(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs)

    End Sub

    Private Sub HandleConStateChanged(ByVal sender As Object, ByVal e As System.Data.StateChangeEventArgs) Handles _MetisCore.ConnectionStateChange
        If e.CurrentState = Data.ConnectionState.Closed OrElse e.CurrentState = Data.ConnectionState.Broken Then
            tvFlows.Nodes.Clear()
            btnFlow_Add.Enabled = False
            btnFlow_Del.Enabled = False
            btnFlow_Exec.Enabled = False
            btnFunc_Open.Enabled = False
        ElseIf e.CurrentState = Data.ConnectionState.Open AndAlso e.OriginalState = Data.ConnectionState.Closed Then
            _FlowList = _MetisCore.Flows
            _FuncList = _MetisCore.Functions
            btnFlow_Add.Enabled = True
            btnReloadFlowList.Enabled = True
            Reload()
        End If
    End Sub

    Public Sub Reload()
        Try
            Me.Cursor = Cursors.WaitCursor
            tvFlows.SuspendLayout()
            tvFlows.Nodes.Clear()
            RemoveHandler _FlowList.ListChanged, AddressOf HandleFlowChildListChange
            _FlowList.Reload()
            For Each item As Core.Flows.IMetisFlowNode In _MetisCore.Flows
                If TypeOf item Is Core.Flows.MetisFlow Then
                    ReloadChildren(item, tvFlows.Nodes)
                ElseIf TypeOf item Is Core.Functions.MetisFunction Then
                    Dim Node As TreeNode = tvFlows.Nodes.Add(item.ID.ToString, item.Name, 1, 1)
                    Node.Tag = item
                    Node.ToolTipText = item.Annotation
                    Node.Nodes.Add("FIELDS", My.Resources.Metis_FuncList_Fields.ToString, 1, 1)
                    Node.Nodes.Add("USES", My.Resources.Metis_FuncList_Uses.ToString, 1, 1)
                    Node.Nodes.Add("USED", My.Resources.Metis_FuncList_Used.ToString, 1, 1)
                End If
            Next
            AddHandler _FlowList.ListChanged, AddressOf HandleFlowChildListChange

        Finally
            tvFlows.ResumeLayout()
            Me.Cursor = Cursors.Default
            btnFlow_Add.Enabled = (tvFlows.SelectedNode Is Nothing OrElse TypeOf tvFlows.SelectedNode.Tag Is Core.Flows.MetisFlow)
            btnFlow_Del.Enabled = (tvFlows.SelectedNode IsNot Nothing)
            btnFunc_Open.Enabled = (tvFlows.SelectedNode IsNot Nothing AndAlso TypeOf tvFlows.SelectedNode.Tag Is Core.Functions.MetisFunction)
            btnFlow_Exec.Enabled = (tvFlows.SelectedNode IsNot Nothing)
        End Try

    End Sub

    Private Sub ReloadChildren(ByVal SrcFlow As Core.Flows.MetisFlow, ByVal DstNodes As TreeNodeCollection)
        Dim RootNode As TreeNode = DstNodes.Add(SrcFlow.ID.ToString, SrcFlow.Name, 0, 0)
        RootNode.Tag = SrcFlow
        RootNode.ToolTipText = SrcFlow.Annotation
        RemoveHandler SrcFlow.ChildListChanged, AddressOf HandleFlowChildListChange
        AddHandler SrcFlow.ChildListChanged, AddressOf HandleFlowChildListChange



        For Each item As Core.Flows.IMetisFlowNode In SrcFlow.Children
            If TypeOf item Is Core.Flows.MetisFlow Then
                ReloadChildren(item, RootNode.Nodes)
            ElseIf TypeOf item Is Core.Functions.MetisFunction Then
                Dim Node As TreeNode = RootNode.Nodes.Add(item.ID.ToString, item.Name, 1, 1)
                Node.Tag = item
                Node.ToolTipText = item.Annotation
                Node.Nodes.Add("FIELDS", My.Resources.Metis_FuncList_Fields.ToString, 1, 1)
                Node.Nodes.Add("USES", My.Resources.Metis_FuncList_Uses.ToString, 1, 1)
                Node.Nodes.Add("USED", My.Resources.Metis_FuncList_Used.ToString, 1, 1)
            End If
        Next
    End Sub

    Private Sub btnFlow_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFlow_Add.Click

    End Sub

    Private Sub btnFlow_Del_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFlow_Del.Click
        If MsgBox(String.Format(My.Resources.Metis_Flow_ConfirmDrop.ToString, DirectCast(tvFlows.SelectedNode.Tag, Core.Flows.MetisFlow).Name), _
          MsgBoxStyle.YesNo Or MsgBoxStyle.Question) = MsgBoxResult.Yes Then
            DirectCast(tvFlows.SelectedNode.Tag, Core.Flows.MetisFlow).Drop()
        End If
    End Sub

    Private Sub btnFlowProperties_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFlowProperties.Click

    End Sub

    Private Sub btnFlow_Exec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFlow_Exec.Click

    End Sub

    Private Sub btnReloadFlowList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReloadFlowList.Click

    End Sub

    Private Sub tvFlows_AfterExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvFlows.AfterExpand

    End Sub

    Private Sub tvFlows_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvFlows.AfterSelect

    End Sub

    Private Sub tvFlows_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles tvFlows.DragDrop

    End Sub

    Private Sub tvFlows_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles tvFlows.DragOver

    End Sub

    Private Sub tvFlows_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvFlows.MouseDown
        Dim tmpNode As TreeNode = tvFlows.GetNodeAt(e.Location)
        tvFlows.SelectedNode = tmpNode

        btnFlow_Add.Enabled = (tmpNode Is Nothing OrElse TypeOf tmpNode.Tag Is Core.Flows.MetisFlow)
        btnFlow_Del.Enabled = (tmpNode IsNot Nothing AndAlso (TypeOf tmpNode.Tag Is Core.Functions.MetisTableFunction OrElse TypeOf tmpNode.Tag Is Core.Flows.MetisFlow))
        btnFlowProperties.Enabled = (tmpNode IsNot Nothing AndAlso TypeOf tmpNode.Tag Is Core.Flows.MetisFlow)
        btnFunc_Open.Enabled = (tmpNode IsNot Nothing AndAlso TypeOf tmpNode.Tag Is Core.Functions.MetisFunction)
        btnFlow_Exec.Enabled = (tmpNode IsNot Nothing AndAlso (TypeOf tmpNode.Tag Is Core.Functions.MetisTableFunction OrElse TypeOf tmpNode.Tag Is Core.Flows.MetisFlow))


        If e.Button = Windows.Forms.MouseButtons.Left AndAlso tmpNode IsNot Nothing Then
            If TypeOf tmpNode.Tag Is Core.Flows.MetisFlow Then
                tvFlows.DoDragDrop(DirectCast(tmpNode.Tag, Core.Flows.MetisFlow), DragDropEffects.Move)
            ElseIf TypeOf tmpNode.Tag Is Core.Functions.MetisFunction Then
                tvFlows.DoDragDrop(DirectCast(tmpNode.Tag, Core.Functions.MetisFunction), DragDropEffects.Move)
            End If
        End If
    End Sub

    Private Sub tvFlows_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tvFlows.NodeMouseDoubleClick
        If TypeOf e.Node.Tag Is Core.Flows.MetisFlow Then
            e.Node.Expand()
        ElseIf btnFunc_Open.Enabled Then
            _ParentForm.OpenMetisEntity(e.Node.Tag)
        End If
    End Sub
End Class
